package com.bruce.asurada.user.controller;

import com.bruce.asurada.common.Result;
import com.bruce.asurada.common.dto.*;
import com.bruce.asurada.user.service.UserService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * 
 * @author Bruce
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@Validated @RequestBody UserRegisterDto registerDto) {
        try {
            userService.register(registerDto);
            return Result.success();
        } catch (Exception e) {
            log.error("用户注册失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserInfoDto> login(@Validated @RequestBody UserLoginDto loginDTO) {
        try {
            UserInfoDto userInfoDto = userService.login(loginDTO);
            return Result.success(userInfoDto);
        } catch (Exception e) {
            log.error("login..获取用户信息失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取用户信息
     */
    @PostMapping("/info")
    public Result<UserInfoDto> getUserInfo(@RequestBody UserQueryDTO queryDTO) {
        try {
            UserInfoDto userInfo = userService.getUserInfo(queryDTO);
            return Result.success(userInfo);
        } catch (Exception e) {
            log.error("getUserInfo...获取用户信息失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}